/*
 * Copyright © 2018 www.noark.xyz All Rights Reserved.
 *
 * 感谢您选择Noark框架，希望我们的努力能为您提供一个简单、易用、稳定的服务器端框架 ！
 * 除非符合Noark许可协议，否则不得使用该文件，您可以下载许可协议文件：
 *
 *        http://www.noark.xyz/LICENSE
 *
 * 1.未经许可，任何公司及个人不得以任何方式或理由对本框架进行修改、使用和传播;
 * 2.禁止在本项目或任何子项目的基础上发展任何派生版本、修改版本或第三方版本;
 * 3.无论你对源代码做出任何修改和改进，版权都归Noark研发团队所有，我们保留所有权利;
 * 4.凡侵犯Noark版权等知识产权的，必依法追究其法律责任，特此郑重法律声明！
 */
package xyz.noark.network.codec.shuttle;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import xyz.noark.core.lang.ByteArray;
import xyz.noark.core.lang.ImmutableByteArray;
import xyz.noark.core.network.NetworkProtocol;
import xyz.noark.network.codec.ByteBufWrapper;
import xyz.noark.network.codec.rpc.RpcPacket;
import xyz.noark.network.rpc.RpcSerializer;
import xyz.noark.network.rpc.shuttle.ShuttleRelayProtocol;
import xyz.noark.network.util.ByteBufUtils;

import java.io.Serializable;
import java.util.Collection;

/**
 * Shuttle封包编码工厂类
 *
 * @author 小流氓[176543888@qq.com]
 * @since 4.0
 */
public class ShuttlePacketCodecFactory {
    private final RpcSerializer serializer;

    public ShuttlePacketCodecFactory(RpcSerializer serializer) {
        this.serializer = serializer;
    }

    // RPC请求 : 包长(4) | 类型(1) | 协议编号(4) | 请求ID(4) | 标记位置(1) | 协议类型(1) | 协议

    public ByteArray encodeRpcPacket(byte type, int reqId, NetworkProtocol np) {
        // 1. 协议部分的序列化
        byte[] body = serializer.serialize(np.getProtocol());

        // 2. 使用CompositeByteBuf组合
        CompositeByteBuf compositeBuf = Unpooled.compositeBuffer();
        compositeBuf.addComponent(true, Unpooled.copyInt(0));
        compositeBuf.addComponent(true, Unpooled.buffer(1).writeByte(type));
        compositeBuf.addComponent(true, Unpooled.copyInt((int) np.getOpcode()));
        compositeBuf.addComponent(true, Unpooled.copyInt(reqId));
        compositeBuf.addComponent(true, Unpooled.buffer(1).writeByte(0));
        compositeBuf.addComponent(true, Unpooled.buffer(1).writeByte(1));
        compositeBuf.addComponent(true, Unpooled.wrappedBuffer(body));

        // 3. 计算总长度并更新包长字段
        compositeBuf.setInt(0, compositeBuf.readableBytes());
        return new ByteBufWrapper(compositeBuf);
    }


    public static ByteArray encodeShuttlePacket(ShuttleRelayProtocol shuttleRelayProtocol) {
        // RPC请求 : 包长(4) | 类型(1) | 协议编号(4) | 玩家ID(8) | 标记位置(1) | 协议类型(1) | 协议

        Collection<Serializable> playerIdList = shuttleRelayProtocol.getPlayerIdList();
        if (playerIdList.size() == 1) {
            //return ShuttlePacketCodecFactory.encodeShuttlePacket(ShuttleType.FORWARD,playerIdList, shuttleProtocol);
        } else {
            return ShuttlePacketCodecFactory.encodeShuttlePacket(shuttleRelayProtocol);
        }


        return null;
    }

    public ShuttleRegisterPacket createRegisterPacket(int totalLength, int serverUid) {
        return new ShuttleRegisterPacket(totalLength, serverUid);
    }

    public RpcPacket createRpcPacket(int totalLength, byte type, ByteBuf byteBuf) {
        // RPC请求 : 包长(4) | 类型(1) | 协议编号(4) | 请求ID(4) | 标记位置(1) | 协议类型(1) | 协议
        RpcPacket packet = new RpcPacket();
        packet.setLength(totalLength);
        packet.setRequestFlag(type == ShuttleType.RPC_REQUEST);
        packet.setOpcode(byteBuf.readInt());
        packet.setReqId(byteBuf.readInt());
        byteBuf.skipBytes(2);
        byte[] bytes = ByteBufUtils.readBytes(byteBuf);

       /* // 压缩标识，0=不压缩，1=GZip压缩,2=XX压缩
        if (compressFlag == 1) {
            try {
                bytes = GzipUtils.uncompress(bytes);
            } catch (IOException ignored) {
                // 出错了就不解了
            }
        }*/

        packet.setByteArray(new ImmutableByteArray(bytes));
        return packet;
    }
}
